// file Upload.java
package com.deneme;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Map;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
import javax.servlet.http.HttpServletResponse;

import com.google.appengine.api.blobstore.BlobKey;
import com.google.appengine.api.blobstore.BlobstoreService;
import com.google.appengine.api.blobstore.BlobstoreServiceFactory;
import com.google.appengine.api.images.Image;
import com.google.appengine.api.images.ImagesServiceFactory;
import com.google.appengine.api.users.User;
import com.google.appengine.api.users.UserService;
import com.google.appengine.api.users.UserServiceFactory;

public class Upload extends HttpServlet {
    private BlobstoreService blobstoreService = BlobstoreServiceFactory.getBlobstoreService();

    public void doPost(HttpServletRequest req, HttpServletResponse res)
        throws ServletException, IOException {

        Map<String, BlobKey> blobs = blobstoreService.getUploadedBlobs(req);
        BlobKey blobKey = blobs.get("myFile");
        
        byte[] imgarr = blobstoreService.fetchData(blobKey, 0, blobstoreService.MAX_BLOB_FETCH_SIZE - 1);
		Image img = ImagesServiceFactory.makeImage(imgarr);
		
		if (img.getHeight() > 800|| img.getWidth() > 800) {
			blobstoreService.delete(blobKey);
			req.setAttribute("errormsg", "Image is too large.  Please only use images less than 800 x 800 pixels.");
			RequestDispatcher rd = req.getRequestDispatcher("/error");
			try {
				rd.forward(req, res);
			} catch (ServletException e) {
				e.printStackTrace();
			}
			return;
		}
        
		UserService userService = UserServiceFactory.getUserService();
		User user = userService.getCurrentUser();
		UserData data = UserData.getDataForUser(user);
		data.addImageKey(blobKey);
		
		System.out.print("User keys: ");
		for(BlobKey bk : data.getImageKeys()) {
			System.out.print(bk.getKeyString() + ", ");
		}

    	if (blobKey == null) {
            res.sendRedirect("/");
        } else {
        	RequestDispatcher rd = req.getRequestDispatcher("/puzzleconfiguration?blob-key=" + blobKey.getKeyString());
        	rd.forward(req, res);
        	
/*        	req.setAttribute("blob-key", blobKey.getKeyString());
        	RequestDispatcher rd = req.getRequestDispatcher("puzzle.jsp");
        	rd.forward(req, res);*/
        	
/*        	ServletContext sc = getServletContext();
        	PrintWriter out = res.getWriter();
        	out.write("<script type=\"text/javascript\">alert(\"Im in ur codez\");</script>");
        	RequestDispatcher rd = sc.getRequestDispatcher("/puzzle.jsp");
        	if(rd == null){System.out.println("rd is null");}
        	if(req == null){System.out.println("req is null");}
        	if(res == null){System.out.println("res is null");}
          	req.setAttribute("blob-key", blobKey.getKeyString());
          	//rd.forward(req, res);
          	res.sendRedirect("/puzzle.jsp?blob-key=" + blobKey.getKeyString());*/
        }
    }
}